package com.baosight.risk.service.fltd.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baosight.risk.api.fltd.AuthoTypeService;
import com.baosight.risk.api.xtzy.IXtzyCompanyService;
import com.baosight.risk.service.fltd.vo.FlaaNodeAuthoTypeVO;
import com.baosight.risk.service.fltd.vo.FltdTaskVO;
import com.baosight.risk.service.xtzy.entity.XtzyCompany;
import com.baosight.risk.service.xtzy.entity.XtzyUser;
import com.baosight.risk.service.xtzy.vo.XtzyCompanyVO;

import lombok.extern.slf4j.Slf4j;

/**
 * 组织机构权限 compRole
 */
@Slf4j
@Service("compRole")
public class CompRoleAuthoTemplate implements AuthoTypeService {

    @Autowired
    private IXtzyCompanyService companyService;

    @Autowired
    private AuthoTypeTemplate authoTypeService;

    @Override
    public Map<String, Object> parse(String template, Map<String, Object> param) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        FlaaNodeAuthoTypeVO authInfo = (FlaaNodeAuthoTypeVO) param.get("authInfo");
        XtzyUser currentUser = (XtzyUser) param.get("currentUser");
        FltdTaskVO currentTask = (FltdTaskVO) param.get("currentTask");
        //流程参与者
        List<XtzyUser> userList = (List<XtzyUser>) param.get("userList");
        String nodeAuth = authInfo.getNodeAuth();
        String roleId = authInfo.getRoleCode();
        XtzyCompany customCompany = currentTask.getCustomCompany();
        List<XtzyUser> newUsers = null;

        String compCode = "";
        String compName = "";
        if (customCompany != null) {
            compCode = customCompany.getCompCode();
            compName = customCompany.getCompName();
        } else {
            compCode = currentTask.getDeptCode();
            compName = currentTask.getDeptName();
        }
        try {
            //全部 or 本单位
            if (AuthoTypeService.COMP_ALL.equals(nodeAuth) || AuthoTypeService.COMP_COMP.equals(nodeAuth)) {
                newUsers = companyService.getUserAndCompIdAndRoleId(compCode, roleId);
                authoTypeService.unionUsers(userList, newUsers);
                if (CollectionUtils.isNotEmpty(userList)) {
                    for (XtzyUser xtzyUser : userList) {
                        xtzyUser.setExt1(compCode);
                        xtzyUser.setExt2(compName);
                    }
                }
            } else {
                List<XtzyCompanyVO> companys = null;
                //本单位及下属单位
                if (AuthoTypeService.COMP_SUB.equals(nodeAuth)) {
                    companys = companyService.getNextComp(compCode, roleId);
                }
                //一级子公司及股份各部门
                else if (AuthoTypeService.COMP1.equals(nodeAuth)) {
                    XtzyCompanyVO parentComp = companyService.getParentComp(compCode, roleId);
                    companys = new ArrayList<>();
                    companys.add(parentComp);
                }
                for (XtzyCompanyVO company : companys) {
                    authoTypeService.addCompany(company.getUserList(), company);
                    authoTypeService.unionUsers(userList, company.getUserList());
                }
            }
        } catch (Exception e) {
            throw new Exception("当前部门："+compName+"，根据组织机构授权关系查找审批人异常！"+e.getMessage());
        }
        return resultMap;
    }
}
